﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Infrastructure;
using MSIACCSL2.Web.WarehouseServices;

namespace MSIACCSL2.Web.ReportPage
{
    public partial class InventoryAdjustment : System.Web.UI.Page
    {
        private DataAccess<Object> DA;
        private ReportDocument _invAdjustment;
        private string Tipe;

        public InventoryAdjustment()
        {
            DA = new DataAccess<object>();
            _invAdjustment = new ReportDocument();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                LoadReport(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
        }


        public void LoadReport(string NoBTB, string tipe)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Select a.NoAdj,a.TglAdjustment,a.[Description],b.SerialNumber,i.NoBundle, ");
            sb.Append("i.QtyPack, b.Panjang,b.Lebar,b.Tinggi,b.QtyUnit,i.KodeMasuk,iw.KodePallet,i.SatuanPack,i.SatuanUnit ");
            sb.Append(",it.ItemName,s.SubGroupName,d.KodeWarehouse  ");
            sb.Append("from InventoryAdjustment a  ");
            sb.Append("INNER JOIN InventoryAdjustmentDetail b ON a.NoAdj = b.NoAdj ");
            sb.Append("INNER JOIN inventory i ON b.SerialNumber = i.SerialNumber ");
            sb.Append("LEFT JOIN (Select SerialNumber,KodeWarehouse,Pallet AS KodePallet from uvw_mutasi_stok) iw ON i.SerialNumber = iw.SerialNumber AND iw.KodeWarehouse=b.KodeWarehouse ");
            sb.Append("Left Join Item it on it.ItemId = i.ItemId    ");
            sb.Append("Left Join Subgroup s on s.SubgroupId = it.SubgroupId  ");
            sb.Append("Left Join Warehouse d on d.KodeWarehouse = b.KodeWarehouse  ");
            sb.AppendFormat("Where a.NoAdj='{0}' Order By s.SubgroupId,i.ItemId ", NoBTB);


            var Source = DA.GetDataListDataSet(sb.ToString());
            string reportPath = Server.MapPath("~/Report/InvAdjustment.rpt");
            _invAdjustment.Load(reportPath);
            _invAdjustment.SetDataSource(Source.Tables[0]);
            var param = new ParameterDiscreteValue();
            param.Value = tipe;
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            _invAdjustment.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
         
            CrystalReportViewer1.ReportSource = _invAdjustment;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            LoadReport(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
        }


        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.Unload += Page_Unload;
        }

        void Page_Unload(object sender, EventArgs e)
        {
            _invAdjustment.Clone();
            _invAdjustment.Dispose();
           
        }
      
    }
}